package com.wlc.service.impl;

import com.wlc.mapper.ResignationStatsMapper;
import com.wlc.pojo.ResignationStats;
import com.wlc.service.ResignationStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ResignationStatsServiceImpl implements ResignationStatsService {

    @Autowired
    private ResignationStatsMapper resignationStatsMapper;

    @Override
    public List<ResignationStats> getDeptResignationStats(String year) {
        log.info("查询部门辞职统计: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }
        return resignationStatsMapper.selectDeptResignationStats(year);
    }

    @Override
    public List<ResignationStats> getMonthlyResignationTrend(String year) {
        log.info("查询月度辞职趋势: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }
        return resignationStatsMapper.selectMonthlyResignationTrend(year);
    }

    @Override
    public List<ResignationStats> getResignReasonStats(String year) {
        log.info("查询辞职原因统计: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }
        return resignationStatsMapper.selectResignReasonStats(year);
    }

    @Override
    public List<ResignationStats> getResignationStatusStats(String year) {
        log.info("查询辞职状态统计: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }
        return resignationStatsMapper.selectResignationStatusStats(year);
    }

    @Override
    public List<ResignationStats> getWorkDurationStats(String year) {
        log.info("查询在职时长统计: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }
        return resignationStatsMapper.selectWorkDurationStats(year);
    }

    @Override
    public ResignationStats getResignationSummary(String year) {
        log.info("查询辞职汇总统计: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }
        return resignationStatsMapper.selectResignationSummary(year);
    }

    @Override
    public List<ResignationStats> getYearlyResignationRate(Integer startYear, Integer endYear) {
        log.info("查询年度辞职率趋势: startYear={}, endYear={}", startYear, endYear);
        if (startYear == null) {
            startYear = LocalDate.now().getYear() - 5;
        }
        if (endYear == null) {
            endYear = LocalDate.now().getYear();
        }
        return resignationStatsMapper.selectYearlyResignationRate(startYear, endYear);
    }

    @Override
    public Object getResignationOverview(String year) {
        log.info("查询辞职统计总览: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }

        Map<String, Object> result = new HashMap<>();
        result.put("deptStats", getDeptResignationStats(year));
        result.put("monthlyTrend", getMonthlyResignationTrend(year));
        result.put("reasonStats", getResignReasonStats(year));
        result.put("statusStats", getResignationStatusStats(year));
        result.put("durationStats", getWorkDurationStats(year));
        result.put("summary", getResignationSummary(year));

        // 计算年度趋势
        Integer currentYear = LocalDate.now().getYear();
        result.put("yearlyTrend", getYearlyResignationRate(currentYear - 5, currentYear));

        return result;
    }
}